package com.bsdsma.fengheguaiapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.bsdsma.fengheguaiapp.dto.CustomerInfoAddDto;
import com.bsdsma.fengheguaiapp.dto.CustomerInfoSearchDto;
import com.bsdsma.fengheguaiapp.dto.DataGroupByDateDto;
import com.bsdsma.fengheguaiapp.entity.CustomerInfo;
import com.bsdsma.fengheguaiapp.enums.Role;
import com.bsdsma.fengheguaiapp.mapper.CustomerInfoMapper;
import com.bsdsma.fengheguaiapp.service.CustomerInfoService;
import com.bsdsma.fengheguaiapp.constants.ErrorCodes;
import com.bsdsma.fengheguaiapp.dto.PageRetDto;
import com.bsdsma.fengheguaiapp.exceptionhandler.GlobalRuntimeException;
import com.bsdsma.fengheguaiapp.utils.CustomerBeanUtils;
import com.bsdsma.fengheguaiapp.utils.DateUtils;
import com.bsdsma.fengheguaiapp.utils.PageRetUtils;
import com.bsdsma.fengheguaiapp.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {

    /**
     * 使用Spring注入Mybatis操作对象
     */
    @Autowired
    private CustomerInfoMapper mapper;

    @Override
    public PageRetDto<CustomerInfo> search(CustomerInfoSearchDto dto) {
        CustomerInfo bean = new CustomerInfo();
        bean.setStatus(0);
        QueryWrapper<CustomerInfo> wrapper = new QueryWrapper<>(bean);

        if (!StringUtils.isBlank(dto.getCustomerName())) {
            wrapper.like("customer_name", "%" + dto.getCustomerName() + "%");
        }
        if (!StringUtils.isBlank(dto.getCustomerPass())) {
            wrapper.like("customer_pass", "%" + dto.getCustomerPass() + "%");
        }
        IPage<CustomerInfo> pageData = mapper.selectPage(new PageDTO<>(dto.getPage(), dto.getSize()), wrapper);
        return PageRetUtils.parse(pageData);
    }

    @Override
    public List<CustomerInfo> listAll() {
        CustomerInfo bean = new CustomerInfo();
        bean.setStatus(0);
        return mapper.selectList(new QueryWrapper<>(bean));
    }

    @Override
    public CustomerInfo add(CustomerInfoAddDto dto) {
        CustomerInfo bean = CustomerBeanUtils.copyPropertiesClass(dto, CustomerInfo.class);
        String customerName = dto.getCustomerName();
        String customerPass = dto.getCustomerPass();
        String customerPhone = dto.getCustomerPhone();
        String customerEmail = dto.getCustomerEmail();

        if (StringUtils.isBlank(customerName)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写顾客账号");
        }
        if (StringUtils.isBlank(customerPass)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写顾客密码");
        }
        if (StringUtils.isBlank(customerPhone)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写顾客电话");
        }
        if (StringUtils.isBlank(customerEmail)) {
            bean.setCustomerEmail("");
        }
        bean.setStatus(0);
        bean.setCt(DateUtils.getNow());
        bean.setUt(DateUtils.getNow());
        mapper.insert(bean);
        return bean;
    }

    @Override
    public CustomerInfo update(Long id, CustomerInfoAddDto dto) {
        CustomerInfo bean = this.getById(id);
        if (null == bean) {
            throw new GlobalRuntimeException(ErrorCodes.OBJ_NOT_EXISTS, "顾客信息不存在");
        }
        String customerName = dto.getCustomerName();
        String customerPass = dto.getCustomerPass();
        String customerPhone = dto.getCustomerPhone();
        String customerEmail = dto.getCustomerEmail();

        if (StringUtils.isBlank(customerName)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写顾客账号");
        }
        bean.setCustomerName(customerName);
        if (StringUtils.isBlank(customerPass)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写顾客密码");
        }
        bean.setCustomerPass(customerPass);
        if (StringUtils.isBlank(customerPhone)) {
            throw new GlobalRuntimeException(ErrorCodes.PARAMS_ERROR, "请填写顾客电话");
        }
        bean.setCustomerPhone(customerPhone);
        if (StringUtils.isBlank(customerEmail)) {
            bean.setCustomerEmail("");
        }else{
            bean.setCustomerEmail(customerEmail);
        }
        mapper.updateById(bean);
        return bean;
    }

    @Override
    public CustomerInfo getById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public void delete(Long id) {
        CustomerInfo bean = this.getById(id);
        if (null == bean) {
            throw new GlobalRuntimeException(ErrorCodes.OBJ_NOT_EXISTS, "顾客信息不存在");
        }
        bean.setStatus(-1);
        bean.setUt(DateUtils.getNow());
        mapper.updateById(bean);
    }

    @Override
    public int updateById(CustomerInfo bean) {
        return mapper.updateById(bean);
    }

    @Override
    public Long count() {
        CustomerInfo bean = new CustomerInfo();
        bean.setStatus(0);
        return mapper.selectCount(new QueryWrapper<>(bean));
    }

    @Override
    public List<DataGroupByDateDto> countByDay(Date beginDate, Date endDate) {
        return mapper.countByDay(beginDate, endDate);
    }
}